ColaboratoryでGoogle Apiを使う
最初は、libraryをつくったけど、簡便でなかった。
なので、手元でやるのが簡便という結論におちついた。
認証とおして、requestを整形(batch対応)するだけなので。
GoogleサービスのAPIは batchRequestに対応してるので、5個までreqを詰める?形にしてる。
code: GADataInColab.py
CLIENT_ID = "xxxx" #@param {type:"string"} CLIENT_SECRET = "xxx" #@param {type:"string"} import httplib2
import math
import itertools
import pandas as pd
from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
ga = build("analytics", "v4", credentials=credentials)
viewId = "ga:xxx" #@param {type:"string"} req = {
"viewId": viewId,
}
res = ga.reports().batchGet(body={"reportRequests":req}).execute() print(n) #check for how many rows df = pd.DataFrame()
for _, nRange in itertools.groupby(range(n), lambda x: math.floor(x/5)):
tmp = []
for i in nRange:
print(i)
print(req)
tmp.append(req.copy())
res = ga.reports().batchGet(body={"reportRequests":tmp}).execute()
for r in res.get("reports", []):
print(df.shape)
names = [z.replace("ga:","") for z in
df.columns = names
# date型を取得してるとして。
df'tm' = pd.to_datetime(df.dateHour + df.minute) dim = "deviceCategory" #@param {type:"string"} (df1
.reset_index()
.pivot(index="tm", columns=dim, values='pageViews')
).plot(figsize=(15,6))